SRC_DIR = ./src
BUILD_DIR = ./build
TARGET = $(BUILD_DIR)/world.out

CC = cc
CFLAGS = -Wall

# ./src/*c
SRCS = $(shell find $(SRC_DIR) -name '*.c')

# ./src/*c => ./build/*.o
OBJS = $(patsubst &(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS))

# ./src/*.c => ./build/*.d
DEPS = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.d, $(SRCS))

all: $(TARGET)


$(BUILD_DIR)/%.d: $(SRC_DIR)/%.c
	@mkdir -p $(dir $@);\
	rm -f $@;\
	$(CC) -MM $< >$@.tmp;\
	sed 's, \($*\)\.o[ :]*, $(BUILD_DIR)/\1.o $@ : ,g' <$@.tmp > $@;\
       	rm -f $@.tmp

# build/xyz.o 的规则由 src/xyz.c 生成
$(BUILD_DIR)/%.o : $(SRC_DIR)/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c -o $@ $<

#链接
$(TARGET):$(OBJS)
	@echo "building $@..."
	@mkdir -p $(dir $@)
	$(CC) -o $(TARGET) $(OBJS)

clean:
	@echo "clean..."
	rm -rf $(BUILD_DIR)

#引入所有 .d文件
-include $(DEPS)
